//
//  MainTabBar.swift
//  SinaWebo
//
//  Created by mis on 16/3/17.
//  Copyright © 2016年 mis. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    lazy var compaseBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    private func setupUI() {
        addSubview(compaseBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let weigth = self.bounds.width / 5
        let height = self.bounds.height
        let rect: CGRect = CGRectMake(0, 0, weigth, height)
        var count: CGFloat = 0;
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRectOffset(rect, weigth * count, 0)
                count += count == 1 ? 2 : 1
            }
        }
        compaseBtn.frame = CGRectOffset(rect, weigth * 2, 0)
        bringSubviewToFront(compaseBtn) // 如果按钮比较大 可实现前置在tool栏的上面
    }
}
